import Vue from 'vue';

import Vuex from 'vuex';

import config from '../../static/js/config.js'

Vue.use(Vuex);

const store = new Vuex.Store({

  state: {
    // 存储token
    Authorization: uni.getStorageSync('Authorization')?uni.getStorageSync('Authorization'):null,
    userInfo:{},
	message_ws:null,
	message_content:[],
	message_list:[],
	message_key:[],
	num:1,
	exit:true,
	scroll:true
  },

  mutations: {
    // 修改token，并将token存入localStorage
    changeLogin (state, token) {

      state.Authorization = token;
	  
	  uni.setStorageSync('Authorization',token)
    },
    getInfo(state,data){
      // 获取用户的信息
      state.userInfo=data

    },clearToken(state){

      // 清除token

      //当token过期或者退出登录调用该方法
      state.Authorization=null;
      
      state.userInfo={}
	  
	  uni.removeStorageSync('Authorization')
	  
	  state.exit = false
	  
	   uni.closeSocket()
	},
	messageSocket(state,data=null){
		uni.connectSocket({
			url: 'ws://127.0.0.1:1234'
		})
		
		uni.onSocketOpen(function (res) {
		  // ws.send()
		  uni.sendSocketMessage({data:JSON.stringify({type:'token',value:state.Authorization})})
		  console.log('连接成功')
		  
		})
		
		uni.onSocketError(function (res) {
			uni.showModal({
			    title: '提示',
			    content: '服务器连接失败',
			    success: function (res) {
			        // if (res.confirm) {
			        //     // state.commit('webSocket')
			        // } else if (res.cancel) {
			        //     console.log('用户点击取消');
			        // }
			    }
			})
		  console.log('');
		})
		
		uni.onSocketClose(function (res) {
		  
		  if(state.exit){
			  uni.showModal({
			      title: '提示',
			      content: '服务器失去响应',
			      success: function (res) {
			      }
			  })
		  }
		  
		})
		
					
		uni.onSocketMessage(function (res) {
			let data =JSON.parse(res.data)
			
			console.log(data)
			
			if(data.type=='tips'){
				if(data.code!=1){
					
					if(data.code==-1){
						uni.showModal({
						    title: '提示',
						    content: data.msg,
						    success: function (res) {
								uni.reLaunch({
									url: '/pages/login/login'
								});
						    }
						})
						
						state.exit = false
						state.Authorization=null;
						
						uni.removeStorageSync('Authorization')
						
						uni.closeSocket()
					}else{
						uni.showModal({
						    title: '提示',
						    content: data.msg,
						    success: function (res) {
								
						    }
						})
					}
					
					
				}
			}else if(data.type=='message'){
				state.message_content.push(data.content)
				
				state.scroll = true
				
				data = data.content
			}else if(data.type=='message_list'){
					// that.data.push(data.content)
					data = data.data
					
					data.sendId = parseInt(data.sendId)
					
					// console.log(data)
					
					let index=state.message_key.indexOf(data.sendId)
					
					// for(var){}
					
					if(index!=-1){
						state.message_key.splice(index,1)
						
						state.message_list.splice(index,1)
						
					}else{
						
						// return
					}
					
					data.class=''
					
					data.hy_src=''
					
					if(data.u_supremeTime > data.time){
						data.class='text_color1'
						data.hy_src='../../static/image/hg.png'
						
					}else if(data.u_memberTime > data.time){
						data.class='text_color2'
						data.hy_src='../../static/image/hy.png'
					}
				
					state.message_key.unshift( data.sendId)
					
					state.message_list.unshift(data)
					
			}
		})
	}
  }
});

export default store;
